<template>
  <div class="data-container">
    <h3>成员管理</h3>

    <el-form
      :inline="true"
      size="medium"
      class="forms"
    >
      <el-form-item label="账户/用户名:">
        <el-input
          v-model="memberForm.username"
          placeholder="请输入"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button
          id="searchBtn"
          type="primary"
          @click="searchStaff"
        >
          <i class="el-icon-search" /> 查询
        </el-button>
      </el-form-item>

      <el-form-item style="float: right">
        <el-button
          id="addBtn"
          type="primary"
          size="medium"
          @click="openSkuDialog"
        >
          <i class="el-icon-plus" /> 新增
        </el-button>
      </el-form-item>
    </el-form>

    <el-row
      type="flex"
      justify="end"
      class="add"
    />

    <el-table
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
      :header-cell-style="{color: '#5373e0',background: '#f3f6fb'}"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="成员序号"
      >
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="账号"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="用户名"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.username">
            <span>{{ scope.row.username }}</span>
          </div>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="姓名"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.fullName">
            <span>{{ scope.row.fullName }}</span>
          </div>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="所属门店"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.storeName }}</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="角色"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.roleNames }}</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="最后一次访问时间"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.lastLoginTime"
            type="info"
          >
            {{ scope.row.lastLoginTime }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        width="120"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="updateClick(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="deleteClick(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <addMember
      :dialog-visible.sync="dialogVisible"
      @refreshList="getList"
    />
    <updateMember
      :dialog-visible.sync="dialogVisible1"
      :member-ids="memberId"
      @refreshList="getList"
    />

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageNo"
      :limit.sync="pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator'
import { Route } from 'vue-router'
import { Dictionary } from 'vuex'
import { login } from '@/api/users'
import { Message, Form as ElForm, Input } from 'element-ui'

import { UserModule } from '@/store/modules/user'
import { getMemberData, delMember } from '@/api/organization.ts'
import addMember from '@/views/organization/addMember.vue'
import Pagination from '@/components/Pagination/index.vue'
import updateMember from '@/views/organization/updateMember.vue'
@Component({
  components: {
    addMember,
    Pagination,
    updateMember
  }
})
export default class extends Vue {
  private list:any[] = []
  private pageNo:number = 1;
  private pageSize:number = 10;
  private dialogVisible:boolean = false;
  private dialogVisible1:boolean = false;
  private total:number = 0
  private memberId:string = ''
  private memberForm = {
    username: ''
  }
  created() {
    this.getList()
  }

  private async getList() {
    let data = await getMemberData(this.pageNo, this.pageSize, this.memberForm)
    this.list = data.result.list
    this.total = data.result.totalCount
  }

  private updateClick(value:any) {
    console.log(value)
    this.memberId = value.id
    this.dialogVisible1 = true
  }
  private openSkuDialog() {
    this.dialogVisible = true
  }

  private searchStaff() {
    this.getList()
  }

  private async deleteClick(value:any) {
    let data = await delMember(value.id)

    if (!data) {
      Message({
        message: '删除失败',
        type: 'error',
        duration: 2 * 1000
      })
    } else {
      Message({
        message: '删除成功',
        type: 'info',
        duration: 2 * 1000
      })
      this.getList()
    }
  }
}
</script>

<style lang="scss">
  .data-container {
    width: 1200px;
    margin: 0 auto;
    padding: 42px 44px;
    background: white;
    height: 100%;
  }
</style>

<style lang="scss" scoped>

</style>
